package com.qijian.maindata.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.qijian.common.enums.ApprovalDescriptionEnum;
import com.qijian.common.exception.ServiceException;
import com.qijian.common.utils.ObjectUtils;
import com.qijian.common.utils.SecurityUtils;
import com.qijian.common.utils.StringUtils;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.pinyin.PinyinUtils;
import com.qijian.common.utils.sync.GetKingDeeData;
import com.qijian.common.utils.uuid.IdUtils;
import com.qijian.maindata.domain.Bom;
import com.qijian.maindata.domain.BomDetail;
import com.qijian.maindata.domain.Item;
import com.qijian.maindata.domain.LineProcessItem;
import com.qijian.maindata.domain.query.BomDetailQuery;
import com.qijian.maindata.domain.query.BomQuery;
import com.qijian.maindata.domain.query.LineProcessItemQuery;
import com.qijian.maindata.domain.vo.*;
import com.qijian.maindata.mapper.BomMapper;
import com.qijian.maindata.service.IBomDetailService;
import com.qijian.maindata.service.IBomService;
import com.qijian.maindata.service.IItemService;
import com.qijian.maindata.service.ILineProcessItemService;
import com.qijian.system.domain.User;
import com.qijian.system.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 生产BOMService业务层处理
 *
 * @author qijian
 * @date 2021-09-01
 */
@Service
@RequiredArgsConstructor
public class BomServiceImpl extends ServiceImpl<BomMapper, Bom> implements IBomService {

    private final BomMapper bomMapper;
    private final IItemService itemService;
    private final IBomDetailService bomDetailService;
    private final ILineProcessItemService lineProcessItemService;

    private final IUserService userService;

    @Override
    public Page<BomVo> pageVo(Page<Bom> page, Wrapper<BomQuery> queryWrapper) {
        return bomMapper.selectVo(page, queryWrapper);
    }

    @Override
    public Page<BomVo4App> list4app(Page<Bom> page, Wrapper<BomQuery> queryWrapper) {
        return bomMapper.list4app(page, queryWrapper);
    }

    @Override
    public Page<BomVo> selectVoForProject(Page<Bom> page, Wrapper<BomQuery> queryWrapper) {
        return bomMapper.selectVoForProject(page, queryWrapper);
    }

    @Override
    public List<BomVo> vo(QueryWrapper<BomQuery> queryWrapper) {
        List<BomVo> list = bomMapper.selectVo(queryWrapper);
        list = list.stream().filter(x -> x.getItem() != null && !Strings.isNullOrEmpty(x.getItem().getItemCode())).collect(Collectors.toList());
        return list;
    }

    @Override
    public String maxVersion(String itemCode) {
        return bomMapper.maxVersion(itemCode);
    }

    @Override
    public List<String> classify() {
        return bomMapper.classify();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean syncKingdeeBom(JSONArray itemData, List<JSONArray> datas, String bomType) {
        //先根据金蝶账户（手机号）查出所有系统账户（手机号）的主键
        Set<String> accounts = datas.stream().map(o -> o.getStr(56)).collect(Collectors.toSet());
        Map<String, Long> userNameIdMap = userService.getUserIdMapByUserName(accounts);
        Bom top;
        boolean isAdd = false;
        Set<String> itemIds = new HashSet<>(datas.size() + 1);
        Map<Long, String> itemAttr = new HashMap<>(datas.size() + 1);
//        List<Item> itemUpdates = new ArrayList<>(16);
        List<BomDetail> bomDetailAdds = new ArrayList<>(16);
        List<BomDetail> bomDetailUpdates = new ArrayList<>(16);
        List<LineProcessItem> consumeAdds = new ArrayList<>(16);
        List<LineProcessItem> consumeUpdates = new ArrayList<>(16);

        Map<String, String[]> createByMap = new HashMap<>(16);
        for (JSONArray wl : datas) {
            createByMap.putIfAbsent(wl.getStr(5), new String[]{wl.getStr(50), wl.getStr(51), wl.getStr(56)});
        }

        Map<Long, BomDetail> bomDataMap = new HashMap<>(datas.size() + 1);
        Map<String, BomDetail> bomMap = new HashMap<>(itemData.size() + datas.size());
        Map<Long, BomDetail> bomDelMap = new HashMap<>(16);
        //所有BOM
        Map<String, LineProcessItem> processItemMap = new HashMap<>(16);
        {
            itemIds.add(itemData.getStr(19));
            String itemCode = itemData.getStr(0);
            Long itemId = itemData.getLong(19);
            String itemCreateBy = itemData.getStr(15);
//            Item item = itemService.getById(itemData.getLong(19));
//            if (ObjectUtils.isNull(item)) {
//                item = new Item();
//                itemAdds.add(item);
//            } else {
//                itemUpdates.add(item);
//            }
//            item.setItemId(itemData.getLong(19));
//            item.setItemCode(itemData.getStr(0));
//            item.setItemName(itemData.getStr(2));
//            item.setDrawingNo(itemData.getStr(20));
//            item.setPartNo(itemData.getStr(21));
//            item.setPartNo(itemData.getStr(20));
//            item.setDrawingNo(itemData.getStr(21));
//            item.setItemUnits(itemData.getStr(4));
//            item.setItemWeight(itemData.getBigDecimal(23));
//            item.setItemVersion(datas.get(0).getStr(35));
//            item.setCreateBy(itemData.getStr(15));
//            item.setCreateTime(itemData.getDate(30));
//            item.setIsDisabled(false);
//            item.setItemClassifyId(itemData.getLong(6));
//            item.setItemClassify(itemData.getStr(7));
//            item.setGroupName(itemData.getStr(7));
//            item.setLetterSearch(PinyinUtils.toFirstChar(item.getItemName()));
//            item.setTypeName("XS");
//            item.setOraCombo(itemData.getStr(28));
            JSONArray attr = new JSONArray(7);
            JSONObject Long = null;
            if (Objects.nonNull(itemData.getBigDecimal(24)) && itemData.getBigDecimal(24).compareTo(BigDecimal.ZERO) > 0) {
                Long = new JSONObject(2);
                Long.set("name", "长");
                Long.set("value", itemData.getBigDecimal(24));
            }
            JSONObject Wide = null;
            if (Objects.nonNull(itemData.getBigDecimal(25)) && itemData.getBigDecimal(25).compareTo(BigDecimal.ZERO) > 0) {
                Wide = new JSONObject(2);
                Wide.set("name", "宽");
                Wide.set("value", itemData.getBigDecimal(25));
            }
            JSONObject High = null;
            if (Objects.nonNull(itemData.getBigDecimal(26)) && itemData.getBigDecimal(26).compareTo(BigDecimal.ZERO) > 0) {
                High = new JSONObject(2);
                High.set("name", "厚");
                High.set("value", itemData.getBigDecimal(26));
            }
            JSONObject Volume = null;
            if (Objects.nonNull(itemData.getBigDecimal(27)) && itemData.getBigDecimal(27).compareTo(BigDecimal.ZERO) > 0) {
                Volume = new JSONObject(2);
                Volume.set("name", "体积");
                Volume.set("value", itemData.getBigDecimal(27));
            }
            JSONObject Perimeter = null;
            if (Objects.nonNull(itemData.getBigDecimal(32)) && itemData.getBigDecimal(32).compareTo(BigDecimal.ZERO) > 0) {
                Perimeter = new JSONObject(2);
                Perimeter.set("name", "周长");
                Perimeter.set("value", itemData.getBigDecimal(32));
            }
            JSONObject Material = null;
            if (Objects.nonNull(itemData.getBigDecimal(29)) && itemData.getBigDecimal(29).compareTo(BigDecimal.ZERO) > 0) {
                Material = new JSONObject(2);
                Material.set("name", "材质");
                Material.set("value", itemData.getBigDecimal(29));
            }
            JSONObject Hole = null;
            if (Objects.nonNull(itemData.getBigDecimal(33)) && itemData.getBigDecimal(33).compareTo(BigDecimal.ZERO) > 0) {
                Hole = new JSONObject(2);
                Hole.set("name", "孔数");
                Hole.set("value", itemData.getBigDecimal(33));
            }
            itemAttr.put(itemId, setAttr(attr, Long, Wide, High, Volume, Perimeter, Material, Hole));

            String[] createBys = createByMap.get(itemCode);
            top = getById(itemId);
            if (ObjectUtils.isNull(top)) {
                bomMapper.recoveryBom(itemId);
                top = getById(itemId);
                if (ObjectUtils.isNull(top)) {
                    isAdd = true;
                    top = new Bom();
                } else {
                    bomDetailService.recoveryBom(itemId);
                }

            }
            BomDetail bomDetail = bomDetailService.getById(itemId);
            if (ObjectUtils.isNull(bomDetail)) {
                bomDetail = new BomDetail();
                bomDetailAdds.add(bomDetail);
            } else {
                bomDetailUpdates.add(bomDetail);
            }
            top.setBomId(itemId);
            top.setItemId(itemId);
            top.setItemCode(itemCode);
            bomDetail.setLevel(0);
            bomDetail.setItemId(itemId);
            bomDetail.setItemCode(itemCode);
            bomDetail.setDetailId(itemId);
            bomDetail.setDetailCode("0");
            bomDetail.setBomId(itemId);
            bomDetail.setPid(null);
            bomDetail.setSubCount(1);
            bomDetail.setQuantity(1);
            bomDetail.setTotalQuantity(BigDecimal.ONE);
            bomDetail.setSort(1);
            bomDetail.setApprovalBy(createBys != null && createBys.length > 1 ? createBys[1] : "");
            bomDetail.setApprovalStatus(ApprovalDescriptionEnum.C);
            bomDetail.setApprovalTime(itemData.getDate(13));
            bomDetail.setProducerId(createBys != null && createBys.length > 2 ? userNameIdMap.get(createBys[2]) : null);
            bomDetail.setProducerName(createBys != null && createBys.length > 0 ? createBys[0] : "");
            top.setCreateBy(itemCreateBy);
            List<User> users = userService.list(new LambdaQueryWrapper<User>()
                    .eq(User::getNickName, itemCreateBy)
                    .select(User::getUserId));
            bomDetail.setCreateId(users.size() > 0 ? users.get(0).getUserId() : null);
            top.setIsDisabled(false);
            top.setBomVersion("1.0");
            if (isAdd) {
                top.setBomStatus("{\"BOM\":true,\"线路\":false,\"定额\":false,\"数控件\":false,\"ECN\":true,\"材料\":false}");
            }
            top.setUserId(SecurityUtils.getUserId());
            top.setNickName(bomDetail.getProducerName());
            // bom增加bom类型 产品,工装,费用 24-03-04
            top.setBomType(bomType);

            bomMap.put(bomDetail.getItemId().toString(), bomDetail);
            BomDetailQuery bomDetailQuery = new BomDetailQuery();
            bomDetailQuery.setBomId(top.getBomId());
            List<BomDetail> bomDataList = bomDetailService.list(WhereEntityTool.invoke(bomDetailQuery));
            final Long bomId = top.getBomId();
            bomDataList.forEach(action -> {
                if (action.getDetailId().compareTo(bomId) != 0) {
                    String pid = action.getPid() == null ? "" : action.getPid().toString();
                    String key = action.getItemId().toString() + pid;
                    if (bomMap.containsKey(key)) {
                        bomDelMap.put(bomMap.get(key).getDetailId(), bomMap.get(key));
                    }
                    bomMap.put(key, action);
                }
            });
            bomMap.remove(itemId.toString());
            bomDataMap.put(itemId, bomDetail);
        }

        Map<Long, Long> itemRepeat = new HashMap<>(16);
        for (JSONArray wl : datas) {
            Long itemId = wl.getLong(16);
            String itemCode = wl.getStr(17);
            String itemName = wl.getStr(18);
            itemIds.add(wl.getStr(16));
//            Item item = itemService.getById(wl.getLong(16));
//            if (ObjectUtils.isNull(item)) {
//                item = new Item();
//                if (ObjectUtils.isNull(itemRepeat.get(wl.getLong(16)))) {
//                    itemAdds.add(item);
//                }
//            } else {
//                if (ObjectUtils.isNull(itemRepeat.get(wl.getLong(16)))) {
//                    itemUpdates.add(item);
//                }
//            }
            itemRepeat.put(itemId, itemId);
//            item.setItemId(wl.getLong(16));
//            item.setItemCode(wl.getStr(17));
//            item.setItemName(wl.getStr(18));
//            item.setPartNo(wl.getStr(39));
//            item.setDrawingNo(wl.getStr(38));
//            item.setItemUnits(wl.getStr(22));
//            item.setItemWeight(wl.getBigDecimal(25));
//            item.setItemVersion(wl.getStr(26));
//            item.setCreateBy(wl.getStr(31));
//            item.setCreateTime(wl.getDate(47));
//            item.setIsDisabled(false);
//            item.setIsDel(false);
//            item.setGroupName(wl.getStr(33));

//            item.setItemClassifyId(wl.getLong(14));
//            item.setItemClassify(wl.getStr(33));
//
//            item.setLetterSearch(PinyinUtils.toFirstChar(item.getItemName()));
//            item.setItemUnits(wl.getStr(22));
            // 单位
//            item.setOraCombo(wl.getStr(40));

            JSONArray attr = new JSONArray(7);
            JSONObject Long = null;
            if (wl.getBigDecimal(41).compareTo(BigDecimal.ZERO) > 0) {
                Long = new JSONObject(2);
                Long.set("name", "长");
                Long.set("value", wl.getBigDecimal(41));
            }
            JSONObject Wide = null;
            if (wl.getBigDecimal(42).compareTo(BigDecimal.ZERO) > 0) {
                Wide = new JSONObject(2);
                Wide.set("name", "宽");
                Wide.set("value", wl.getBigDecimal(42));
            }
            JSONObject High = null;
            if (wl.getBigDecimal(43).compareTo(BigDecimal.ZERO) > 0) {
                High = new JSONObject(2);
                High.set("name", "厚");
                High.set("value", wl.getBigDecimal(43));
            }
            JSONObject Volume = null;
            if (wl.getBigDecimal(44).compareTo(BigDecimal.ZERO) > 0) {
                Volume = new JSONObject(2);
                Volume.set("name", "体积");
                Volume.set("value", wl.getBigDecimal(44));
            }
            JSONObject Perimeter = null;
            if (wl.getBigDecimal(52).compareTo(BigDecimal.ZERO) > 0) {
                Perimeter = new JSONObject(2);
                Perimeter.set("name", "周长");
                Perimeter.set("value", wl.getBigDecimal(52));
            }
            JSONObject Material = null;
            if (wl.getStr(45) != null) {
                Material = new JSONObject(2);
                Material.set("name", "材质");
                Material.set("value", wl.getStr(45));
            }
            JSONObject Hole = null;
            if (wl.getBigDecimal(55).compareTo(BigDecimal.ZERO) > 0) {
                Hole = new JSONObject(2);
                Hole.set("name", "孔数");
                Hole.set("value", wl.getBigDecimal(55));
            }
            itemAttr.put(itemId, setAttr(attr, Long, Wide, High, Volume, Perimeter, Material, Hole));
//            item.setItemSpec(wl.getStr(49));

            String type = wl.getStr(21);
            BomDetail parent = bomDataMap.get(wl.getLong(4));
//            if ("1".equals(type)) {
//                item.setTypeName("WG");
//            } else
            if ("2".equals(type)) {
//                item.setTypeName("ZZ");
                String[] createBys = createByMap.get(itemCode);
                String pid = parent.getDetailId() == null ? "" : parent.getDetailId().toString();
                BomDetail bom = bomMap.remove(itemId.toString() + pid);
                if (bom == null || bom.getBomId() == null) {
                    bom = new BomDetail();
                    bom.setDetailId(IdUtils.singletonSnowId());
                    bomDetailAdds.add(bom);
                } else {
                    bomDetailUpdates.add(bom);
                }
                bomDataMap.put(itemId, bom);
                bom.setItemId(itemId);
                bom.setItemCode(itemCode);
                bom.setPid(parent.getDetailId());
                bom.setBomId(top.getBomId());
                bom.setSubCount(1);
                bom.setQuantity(wl.getBigDecimal(28).intValue());
                bom.setTotalQuantity(parent.getTotalQuantity().multiply(BigDecimal.valueOf(bom.getQuantity())));
                Integer sort = wl.getInt(37);
                if (sort == null) {
                    throw new ServiceException("E01:物料编号：" + itemCode + "；物料名称：" + itemName + ",项次为空！");
                }
                bom.setDetailCode(StringUtils.isNotBlank(parent.getDetailCode()) ? (parent.getDetailCode().equals("0") ? "" : parent.getDetailCode() + ".") + sort : sort.toString());
                bom.setLevel(bom.getDetailCode().split("\\.").length);
                bom.setSort(sort);
                bom.setIsDel(false);
                bom.setApprovalBy(createBys != null && createBys.length > 1 ? createBys[1] : "");
                bom.setApprovalStatus(ApprovalDescriptionEnum.C);
                bom.setApprovalTime(wl.getDate(48));
                bom.setCreateBy(createBys != null && createBys.length > 0 ? createBys[0] : "");
                List<User> users = userService.list(new LambdaQueryWrapper<User>()
                        .eq(User::getNickName, createBys != null && createBys.length > 0 ? createBys[0] : "")
                        .select(User::getUserId));
                bom.setCreateId(users.size() > 0 ? users.get(0).getUserId() : null);
                bom.setCreateTime(wl.getDate(47));
                bom.setProducerId(userNameIdMap.get(wl.getStr(56)));
                bom.setProducerName(wl.getStr(50));
            }

            // 查询BOM用料
            LineProcessItemQuery processItemQuery = new LineProcessItemQuery();
            processItemQuery.setBomId(parent.getDetailId());
            List<LineProcessItem> lineProcessItemList = lineProcessItemService.list(WhereEntityTool.invoke(processItemQuery));
            lineProcessItemList.forEach(action -> {
                if (action.getBomId() != null && action.getItemId() != null) {
                    processItemMap.put(action.getBomId().toString() + action.getItemId().toString(), action);
                }
            });

            // 用料
            LineProcessItem lineProcessItem = processItemMap.remove(parent.getDetailId().toString() + itemId.toString());
            if (ObjectUtils.isNull(lineProcessItem)) {
                lineProcessItem = new LineProcessItem();
                lineProcessItem.setDemandId(IdUtils.singletonSnowId());
                consumeAdds.add(lineProcessItem);
            } else {
                consumeUpdates.add(lineProcessItem);
            }
            lineProcessItem.setBomId(parent.getDetailId());
            lineProcessItem.setItemId(itemId);
            lineProcessItem.setItemCode(itemCode);
            lineProcessItem.setItemName(itemName);
            lineProcessItem.setQuantity(wl.getBigDecimal(28));
            lineProcessItem.setApprovalBy(wl.getStr(51));
            lineProcessItem.setApprovalStatus(ApprovalDescriptionEnum.C);
            lineProcessItem.setApprovalTime(wl.getDate(48));
            lineProcessItem.setProducerName(wl.getStr(50));
            lineProcessItem.setCreateTime(wl.getDate(47));

            JSONObject spec = new JSONObject();
            if (StringUtils.isNotBlank(wl.getStr(29))) {
                spec.set("name", "规格");
                spec.set("value", wl.getStr(29));
                attr.add(spec);
            }
            JSONObject cz = new JSONObject(2);
            if (StringUtils.isNotBlank(wl.getStr(53))) {
                cz.set("name", "材质");
                cz.set("value", wl.getStr(53));
                attr.add(cz);
            }
            lineProcessItem.setRemark(JSONUtil.toJsonStr(attr));
        }

        // 物料信息
        if (CollectionUtil.isNotEmpty(itemIds)) {
            // 添加
//            itemService.saveBatch(itemAdds);
            List<com.alibaba.fastjson.JSONObject> jsonObjectList = new GetKingDeeData().getItemByIds(itemIds);
            List<Item> itemList = jsonObjectList.stream().map(BomServiceImpl::kingdeeItem2ItemBean).collect(Collectors.toList());
            itemList.forEach(a -> a.setItemAttr(itemAttr.get(a.getItemId())));
            itemService.saveOrUpdateBatch(itemList);
        }

        // BOM
        boolean check = isAdd ? save(top) : updateById(top);
        // BOM明细
        if (CollectionUtil.isNotEmpty(bomDetailAdds)) {
            //添加
            bomDetailService.saveBatch(bomDetailAdds);
        }
        if (CollectionUtil.isNotEmpty(bomDetailUpdates)) {
            //修改
            bomDetailService.updateBatchById(bomDetailUpdates);
        }
        if (CollectionUtil.isNotEmpty(bomDelMap) || CollectionUtil.isNotEmpty(bomMap)) {
            //删除
            Set<Long> ids = new HashSet<>(bomDelMap.size());
            bomDelMap.forEach((key, value) -> ids.add(value.getDetailId()));
            bomMap.forEach((key, value) -> ids.add(value.getDetailId()));
            bomDetailService.removeByIds(ids);
        }
        // 用料
        if (CollectionUtil.isNotEmpty(consumeAdds)) {
            //添加
            lineProcessItemService.saveBatch(consumeAdds);
        }
        if (CollectionUtil.isNotEmpty(consumeUpdates)) {
            //修改
            lineProcessItemService.updateBatchById(consumeUpdates);
        }
        return check;
    }

    @Override
    @Transactional
    public boolean removeWithDetails(List<Long> bomIds) {
        BomDetailQuery query = new BomDetailQuery();
        query.setBomIds(new HashSet<>(bomIds));
        return this.removeByIds(bomIds) && bomDetailService.remove(WhereEntityTool.invoke(query));
    }

    @Override
    public BomVo getBomAllLineProcessItems(Long bomId) {
        BomVo bom = bomMapper.selectBomAllLineProcessItems(bomId);
        if (bom != null) {
            List<BomDetailVo> bomDetails = bom.getBomDetails();
            if (CollectionUtil.isNotEmpty(bomDetails)) {
                Iterator<BomDetailVo> it = bomDetails.iterator();
                while (it.hasNext()) {
                    BomDetailVo bomDetail = it.next();
                    if (CollectionUtil.isNotEmpty(bomDetail.getProcessList())) {
                        LineProcessVo first = bomDetail.getProcessList().get(0);
                        if (!"XL".equals(first.getProcessType())) {
                            it.remove();
                            continue;
                        }
                        LineProcessItemQuery processItemQuery = new LineProcessItemQuery();
                        processItemQuery.setBomId(bomDetail.getDetailId());
                        List<LineProcessItem> lineProcessItemList = lineProcessItemService.list(WhereEntityTool.invoke(processItemQuery));
                        for (LineProcessVo process : bomDetail.getProcessList()) {
                            if ("XL".equals(process.getProcessType()) && CollectionUtil.isEmpty(process.getProcessItemList()) && CollectionUtil.isNotEmpty(lineProcessItemList)) {
                                process.setProcessItemList(lineProcessItemList);
                            }
                        }
                    }
                }
            }
        }
        return bom;
    }

    private String setAttr(JSONArray attr, JSONObject aLong, JSONObject wide, JSONObject high, JSONObject volume, JSONObject perimeter, JSONObject material, JSONObject hole) {
        if (aLong != null) {
            attr.add(aLong);
        }
        if (wide != null) {
            attr.add(wide);
        }
        if (high != null) {
            attr.add(high);
        }
        if (volume != null) {
            attr.add(volume);
        }
        if (perimeter != null) {
            attr.add(perimeter);
        }
        if (material != null) {
            attr.add(material);
        }
        if (hole != null) {
            attr.add(hole);
        }
        if (wide != null) {
            attr.add(wide);
        }
        return JSONUtil.toJsonStr(attr.size() == 0 ? null : attr);
    }

    public static Item kingdeeItem2ItemBean(com.alibaba.fastjson.JSONObject i) {
        Item item = new Item();
        item.setItemId(i.getLong("FMATERIALID"));
        item.setItemCode(i.getString("FNumber"));
        item.setItemName(i.getString("FName"));
        item.setLetterSearch(PinyinUtils.toFirstChar(item.getItemName()));
        item.setItemClassify(i.getString("FMaterialGroup.FName"));
        item.setItemClassifyId(i.getLong("FMaterialGroup"));
        item.setItemSpec(i.getString("FSpecification"));
        item.setItemSource(i.getString("FMaterialSRC"));
        item.setItemUnits(i.getString("FBaseUnitId.FName"));
        item.setItemUnitSup(i.getString("FAuxUnitID.FName"));
        item.setItemVersion("1.0");
        item.setItemPrice(i.getBigDecimal("FPurPrice_CMK"));
        item.setMinPackage(i.getBigDecimal("FIncreaseQty"));
        item.setSafeStock(i.getBigDecimal("FPlanSafeStockQty"));
        item.setWorkshopId(i.getLong("FWorkShopId"));
        item.setWorkshopName(i.getString("FWorkShopId.FName"));
        item.setStoreId(i.getLong("FStockId"));
        item.setStoreName(i.getString("FStockId.FName"));
        item.setDefaultCurrency(i.getString("FCurrencyId.FName"));
        item.setItemWeight(i.getBigDecimal("FNETWEIGHT"));
        item.setGrossWeight(i.getBigDecimal("FGROSSWEIGHT"));
        item.setTypeName(i.getString("FERPCLSID"));
        item.setGroupName(item.getItemClassify());
        item.setOraCombo(i.getString("F_ora_Combo"));
        item.setQaQuantity(i.getInteger("FExpPeriod"));
        item.setQaType(i.getString("FExpUnit"));
        item.setMoq(i.getBigDecimal("FOrderQty"));
        item.setPartNo(i.getString("F_ORA_piece"));
        item.setDrawingNo(i.getString("F_ORA_figure"));
        item.setIsCheck(i.getBoolean("FCHECKINCOMING"));
        item.setIsSale(i.getBoolean("FIsSale"));
        item.setIsPurchase(i.getBoolean("FIsPurchase"));
        item.setIsStock(i.getBoolean("FIsInventory"));
        item.setIsDisabled(!"A".equals(i.getString("FForbidStatus")));
        item.setApprovalBy(i.getString("FApproverId.FName"));
        item.setApprovalTime(i.getDate("FApproveDate"));
        item.setApprovalStatus(ApprovalDescriptionEnum.valueOf(i.getString("FDocumentStatus")));
        item.setCreateBy(i.getString("FCreatorId.FName"));
        item.setCreateTime(i.getDate("FCreateDate"));
        item.setUpdateBy(i.getString("FModifierId.FName"));
        item.setUpdateTime(i.getDate("FModifyDate"));
        return item;
    }


    @Override
    public List<BomReminderVo> getReminderRecords() {

        return bomMapper.getReminderRecords();
    }

    @Override
    public Page<BomVo> selectVoForDESQSLKJ(Page<Bom> page, Wrapper<BomQuery> queryWrapper, String startTime) {
        return bomMapper.selectVoForDESQSLKJ(page, queryWrapper, startTime);
    }

    @Override
    public int updateBomLine(List<Long> projectIds) {
        return bomMapper.updateBomLine(projectIds);
    }
}
